
	Ext.namespace("panel.widgets.chunks.grid");
	
	panel.widgets.chunks.grid.search = function(config) {
		
		var config = Ext.value(config, {});
		
		var widget = {};
		var state  = {};
		
		state.config = {
			
			hidden:    		  Ext.value(config.hidden       , false),
			searchText:       Ext.value(config.title        , "Search:"),
			position:         Ext.value(config.position     , "top"),
			iconCls:          Ext.value(config.iconCls      , "icon_action_search"),
			width:            Ext.value(config.width        , 400),
			mode:             Ext.value(config.mode         , "local"),
			disableMenu:      Ext.value(config.disableMenu  , false),
			menuStyle:        Ext.value(config.menuStyle    , undefined),
			minChars:         Ext.value(config.minChars     , undefined),
			
			checkIndexes:     Ext.value(config.checkIndexes     , []),
			disableIndexes:   Ext.value(config.disableIndexes   , []),
			
			toolbarContainer: Ext.value(config.toolbarContainer , undefined),
			
			listeners: Ext.value(config.listeners, {}),
			
			autoFocus: false,
			
			paramNames: { 
				fields : "filter_by_fields", 
				query  : "filter" 
			}
			
		};
		
		state.config = $H(state.config);
		
		widget.widget = new Ext.ux.grid.Search(state.config);
		
		if( config.highlight != undefined ) {
			
			state.get_highlight_dom = function(v) {
				
				if( v instanceof Function ) {
					return config.highlight(); }
					
				return v;
								
			}	// function
			
			widget.widget.on("onBeforeSearch", function() {
				
				/*
				panel.console.log(
					"PW:chunks:grid:search", 
					"->E", "remove highlight"
				);
				*/	
				
				panel.ui.highlight.remove(
					state.get_highlight_dom(config.highlight), 
					"s_highlight"
				);
					
			});
			
			widget.widget.on("onSearch", function(me, value) {
				
				/*
				panel.console.log(
					"PW:chunks:grid:search", 
					"->E", "apply highlight"
				);
				*/
				
				if( config.highlightSeparator ) {
					
					var hi = value.split(config.highlightSeparator);
					
				} else {
					
					var hi = [value];
					
				}	// if

				hi.each( function(hv) {
					
					panel.ui.highlight.apply(
						state.get_highlight_dom(config.highlight),
						hv, "s_highlight", true
					);
					
				});
			
			});
			
		}	// if
		
		widget.addSync = function(sync) {
			
			widget.widget.on("onSearch", function(widget, value) {

				sync.field.setValue(value);
				sync.onTriggerSearch();
				
			});
			
			widget.widget.on("onAfterClear", function() {

				sync.onTriggerClear();
				
			});
			
		};
				
		return widget;
		
	};
